// component/mySwiper/mySwiper.js
const app = getApp();
const util = require("../../utils/util.js");
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    showLogin: {
      type: Boolean,
      value: false,
    },
    avatar:{
      type: String,
      value: '',
    },
    nickname:{
      type: String,
      value: '',
    },
    hidecd: {
      type: Boolean,
      value: false,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    current:0,
    avatar:'',
    showLogin:false,
    avatar:'',
    nickname:'',
    hidecd:false
  },
  // 生命周期
  ready(callback){
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 页面跳转函数
    _navTo: function (e) {
      let url,tabPage;
      url = e.currentTarget.dataset.url;
      tabPage = e.currentTarget.dataset.tabPage;
      if (!url) return
      if (app.globalData.constructor != Object) app.globalData = {};
      app.globalData.query = undefined;
      if (tabPage) {
        let recombine, query, [page, queryStr] = url.split('?');
        recombine = (str) => {
          if (!str) return false;
          let cur, obj = {},
            arr = str.split('&');
          for (cur of arr) {
            let [name, value] = cur.split('=');
            if (!obj[name]) obj[name] = value
            else if (Array.isArray(obj[name])) obj[name].push(value)
            else obj[name] = [obj[name], value]
          }
          return obj;
        }
        query = recombine(queryStr);
        if (query) this.globalData.query = JSON.stringify(query)
        wx.switchTab({
          url: page,
        })
      } else {
        wx.navigateTo({
          url: url,
        })
      }
    },

    // 跳转网页
    toWeb(e){
      let url = e.currentTarget.dataset.url
      wx.navigateTo({
        url: '/pages/web/web?url='+encodeURIComponent(url),
      })
    },
    // 关闭弹窗
    close(e){
      let _this = this
      _this.setData({
        showLogin:false
      })
      if(!_this.data.hidecd){
        wx.showTabBar({
          animation: true,
        })
      }
      app.globalData.invite_code=''
      wx.clearStorageSync()
    },
    getUserProfile(e) {
      let _this = this
      if(_this.data.avatar==''){
        app.showMsg('请上传头像')
        return false
      }
      console.log(_this.data.nickname)
      if(_this.data.nickname==''){
        app.showMsg('请填写您的昵称')
        return false
      }
      app._post_form('member/save_info', {
        avatar: _this.data.avatar,
        username: _this.data.nickname
      }, function(result) {
        var pages = getCurrentPages();
        var beforePage = pages[0];
        // wx.navigateBack()
        beforePage.onLoad()
        beforePage.onShow()
        _this.setData({
          showLogin:false
        })
        if(!_this.data.hidecd){
          wx.showTabBar({
            animation: true,
          })
        }
      });
    },
    //  获取标签
    getNickname(e){
      console.log(e.detail.value)
      if(e.detail.value.length<5){
        this.setData({nickname:e.detail.value});
      }else{
        this.setData({nickname:e.detail.value.substring(0, 4)});
      }
    },
    // 获取头像
    bindchooseavatar(e){
      let that = this
      console.log(e.detail.avatarUrl)
      util.uploadImage(e.detail.avatarUrl).then(res => {
        that.setData({
          avatar: res
        })
        console.log(that.data.avatar)
      });
    }
  }
})